package com.formation.userbase.persistance.dao;

import java.sql.Connection;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.sql.Statement;
import java.util.ArrayList;
import java.util.List;

import com.formation.userbase.persistance.beans.User;
import com.formation.userbase.persistance.utils.BdConnect;

public class UserJdbcDao implements UserDao {
	BdConnect bd =new BdConnect();

	@Override
	public User findUserByLogin(String login) {
		Connection connection = null;
		ResultSet result = null;
		User u = new User();
		
		  connection = bd.getConnexion();
		  
		  try{
		      if(connection.isClosed()){return null;}
		      result = connection.createStatement().executeQuery("SELECT * FROM utilisateur WHERE login='"+login+"'");
		      result.next();
		      
		      u.setCreation(result.getDate("date_creation"));
		      u.setEstAdmin(result.getBoolean("estAdmin"));
		      u.setLogin(result.getString("login"));
		      u.setNaiss(result.getDate("date_naissance"));
		      u.setMdp(result.getString("mdp"));
		      u.setNom(result.getString("nom"));
		      u.setPrenom(result.getString("prenom"));
		      u.setSecu(result.getLong("no_secu"));
		      /*String nom = result.getString("nom");
		      String prenom = result.getString("prenom");
		      String logi = result.getString("login");
		      String pass = result.getString("password");*/
		      
		      connection.close();
		   }catch(SQLException sqle){
			  sqle.printStackTrace();
		      return null;
		   }
		   
		   finally{
		      if(result!=null){try{result.close();}catch(Exception e){}}
		   }  
		  
		return u;
	}

	@Override
	public boolean insert(User u) {
		// TODO Auto-generated method stub
		String CmdSQL="Insert into utilisateur (nom,prenom,date_naissance,no_secu,estAdmin,date_creation,login,mdp) values ('"+u.getNom()+"','"+u.getPrenom()+"','"+u.getNaiss()+"',"+u.getSecu()+","+u.isEstAdmin()+",CURDATE(),'"+u.getLogin()+"','"+u.getMdp()+"')";
	//	String CmdSQL="Insert into utilisateur (nom,prenom,date_naissance,no_secu,estAdmin,date_creation,login,mdp) values ('toto','toto','1980-12-12',2222222,true,CURDATE(),'toto','null')";
		System.out.println(CmdSQL);
		if(execMAJ(CmdSQL))
			return true;
		else
			return false;
		 
	      
	}

	boolean execMAJ(String CmdSQL){
	boolean ok=true;
	Connection	connection = bd.getConnexion();
	try {
		Statement st=connection.createStatement();
		st.executeUpdate(CmdSQL);
		st.close();
		connection.close();
	} catch (SQLException e) {
		// TODO Auto-generated catch block
		e.printStackTrace();
		ok=false;
	}
		return ok;
	}
	
	public List<User> getAllUser(){
		List<User> lUser= new ArrayList();
		Connection connection = null;
		ResultSet result = null;
		User u ;
		 connection = bd.getConnexion();
		  
		  try{
		      if(connection.isClosed()){return null;}
		      result = connection.createStatement().executeQuery("SELECT * FROM utilisateur ");
		      result.next();
		      while (!result.isAfterLast()){
		     // System.out.println("liste +1");
		      u = new User();
		      u.setCreation(result.getDate("date_creation"));
		      u.setEstAdmin(result.getBoolean("estAdmin"));
		      u.setLogin(result.getString("login"));
		      u.setNaiss(result.getDate("date_naissance"));
		      u.setMdp(result.getString("mdp"));
		      u.setNom(result.getString("nom"));
		      u.setPrenom(result.getString("prenom"));
		      u.setSecu(result.getLong("no_secu"));
		      lUser.add(u);
		      result.next();
		      }
		      
		      connection.close();
		   }catch(SQLException sqle){
			  sqle.printStackTrace();
		      return null;
		   }
		   
		   finally{
		      if(result!=null){try{result.close();}catch(Exception e){}}
		   }  
		
		return lUser;
		
	}
}
